﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.BusinessLogicLayer;

public partial class DeviceManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /*对进行查询的设备类型下拉框各项进行设置*/
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("logout.aspx");
            }
            //ListItem li = new ListItem("请选择", "");
            //DataSet ds = (new DeviceType()).GetDeviceType();
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    li = new ListItem(dr["deviceTypeName"].ToString(), dr["deviceTypeName"].ToString());
            //    this.DeviceType.Items.Add(li);
            //}
            DataSet ds = (new DeviceType()).GetDeviceType();
            DeviceType.DataSource = ds;
            DeviceType.DataTextField = "deviceTypeName";
            DeviceType.DataValueField = "deviceTypeName";
            DeviceType.DataBind();
            DeviceType.Items.Insert(0, new ListItem("请选择", ""));

            ds = (new Department()).GetDepartMentName();
            departmentName.DataSource = ds;
            departmentName.DataTextField = "departmentName";
            departmentName.DataValueField = "departmentName";
            departmentName.DataBind();
            departmentName.Items.Insert(0, new ListItem("请选择", ""));

            //this.GridView1.DataSource = (new DeviceInfo()).GetDevice();
            //this.GridView1.DataBind();
            Btn_Query_Click(null, null);
        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标选择某行时变颜色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00ffee';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
            //e.Row.Cells[6].Text = Convert.ToDateTime(e.Row.Cells[6].Text).ToShortDateString();
        }
    }

    protected void Btn_Query_Click(object sender, EventArgs e)
    {
        /*取得各个查询字段的内容然后调用业务类的QueryDevice函数进行查询*/
        //string deviceName = this.DeviceName.Text;
        string deviceType = this.DeviceType.Text;
        string dptName = this.departmentName.Text;
        //string deviceNumber = this.deviceNumber.Text;
        //string startTimeString = this.StartTime.Text;
        //string endTimeString = this.EndTime.Text;
        string wn = this.txtWindowNumber.Text;
        string ipAddress = this.txtIPAddress.Text;
        string dm = txtDeviceDemo.Text;

        this.GridView1.DataSourceID = null;
        this.GridView1.DataSource = (new DeviceInfo()).QueryDevice(wn, deviceType, dptName, ipAddress, dm);
        this.GridView1.DataBind();

    }

    //protected void cbAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    CheckBox cb = sender as CheckBox;
    //    for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
    //    {
    //        CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbox");
    //        if (cb.Checked == true)
    //        {
    //            cbox.Checked = true;
    //        }
    //        else
    //        {
    //            cbox.Checked = false;
    //        }
    //    }
    //}

    protected void btnDel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("cbox");
            if (cbox.Checked == true)
            {
                string docid = GridView1.DataKeys[i].Value.ToString();
                string outInfo = (new DeviceInfo()).DelDeviceById(docid);
                //Response.Write("<script>alert('" + outInfo + "');</script>");
            }
        }
        Response.Redirect("~/DeviceManage.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Btn_Query_Click(null, null);
    }
}
